using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Core;
using System.Linq;

namespace WordSearcher
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboDictionary.Items.Add(new Wordlist(@"word.list.common") { Name = "Common Dictionary" });
            comboDictionary.Items.Add(new Wordlist(@"word.list.gsl") { Name = "General Dictionary" });
            comboDictionary.Items.Add(new Wordlist(@"word.list.big") { Name = "Big Dictionary" });
            comboDictionary.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnGenerateSolve.Enabled = false;
            Board board = new Board(9, (((2 * 9) - 1) * 3) + 9);
            UpdateImage(board);
            wordlist.Text = "";

            var finder = new SimpleWordFinder(board, (Wordlist)comboDictionary.Items[comboDictionary.SelectedIndex]);
            var watch = Stopwatch.StartNew();
            int countFoundWords = 0;
            labelTiming.Text = "Working...";

            Application.DoEvents();

            finder.OnFinished += (foundwords =>
                                 {
                                     btnGenerateSolve.Enabled = true;
                                     watch.Stop();
                                     labelTiming.Text = watch.ElapsedMilliseconds + "ms";
                                     wordlist.Text = "";
                                     foreach (var foundword in foundwords.Select(y => new
                                                                                     {
                                                                                         word = board.MovesToWord(y),
                                                                                         score = Scorer.GetScore(board, y)
                                                                                     })
                                                                          .OrderBy(z => z.score))
                                     {
                                         wordlist.Text = foundword.word + " [" + foundword.score + "pts]" + Environment.NewLine + wordlist.Text;
                                     }
                                 });
            finder.OnWordFound += delegate(List<int> moves)
                                 {
                                     countFoundWords++;
                                     wordlist.Text = board.MovesToWord(moves) + "\r\n" + wordlist.Text;
                                     labelWords.Text = countFoundWords + " words";
                                     Application.DoEvents();
                                 };
            finder.FindAllWords();
        }

        private void UpdateImage(Board board)
        {
            if (board != null)
                canvas.Image = board.GetBitmap(canvas.Width, canvas.Height);
        }
    }
}
